//
//  SampuxAddEquipmentSucessView.m
//  Sampux
//
//  Created by rzm on 16/7/11.
//  Copyright © 2016年 rzm. All rights reserved.
//

#import "SampuxAddEquipmentSucessView.h"


@interface SampuxAddEquipmentSucessView ()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *nameText ;

@end
@implementation SampuxAddEquipmentSucessView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setEquipAlisa:) name:NOTIFICATION_RESPONSE_DEVICE_RENAME object:nil];

        [self setupSubView];
    }
    return self;
}


- (void)setupSubView
{
    CGFloat font = 8;
    if (IS_IPHONE_4_OR_LESS) {
        font = 14.;
    }else if (IS_IPHONE_5_OR_LESS)
    {
        font = 14.;
    }else if (IS_IPHONE_6)
    {
        font = 16.;
    }else
    {
        font = 17.;
    }
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(self.width/2 - 40, Sampux6HScale(40)+44 , 80, Sampux6HScale(30))];
    lab.text = @"添加成功!";
    [self addSubview:lab];
    lab.textColor = THWhiteAlplaColor(0.6);
    lab.textAlignment = NSTextAlignmentCenter;

    UIButton *backBtn = [UIButton createButton];
    [self addSubview:backBtn];
    backBtn.frame = CGRectMake(lab.right + 10, Sampux6HScale(10)+44, 40 , 40);
    [backBtn setImage:imageNamed(@"off_success") forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:UIControlEventTouchUpInside];
//    backBtn.backgroundColor = RandomColor;
    
    
    UIButton *imgBtn = [UIButton createButton];
    [self addSubview:imgBtn];
    imgBtn.frame = CGRectMake(self.width/2 - 130/4, lab.bottom, 130/2, 130/2);
    [imgBtn setImage:imageNamed(@"success") forState:UIControlStateNormal];
    
    _nameText = [UITextField createTextFieldWithRect:CGRectMake(self.width/2 - Sampux6WScale(SampuxCustomW)/2, imgBtn.bottom  + Sampux6HScale(40), Sampux6WScale(SampuxCustomW), Sampux6HScale(SampuxCustomH)) andPlaceholder:@"给设备起个别名吧" andLeftImage:@"make-user"];
    [self addSubview:_nameText];
    [UIView createLineViewInbottom:_nameText];
    _nameText.delegate = self;
    
    UIButton *btn = [UIButton createButtonWithTitle:@"确定" buttonType:UIButtonTypeCustom frame:CGRectMake(self.width/2 - Sampux6WScale(345/2)/2, _nameText.bottom + SampuxMargin *2, Sampux6WScale(345/2),Sampux6HScale(SampuxCustomH)) titleFont:[UIFont systemFontOfSize:font] titleColor:THDeFaultBlueColor backColor:THSystemClearColor];
    [self addSubview:btn];
    btn.layer.borderWidth = 1;
    btn.layer.borderColor = THWhiteAlplaColor(0.6).CGColor;
    [btn setCorner:btn.height/2];
    [btn addTarget:self action:@selector(makeSureBtnClick) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = THFont(font + 1);

}

- (void)makeSureBtnClick
{
    [GBInterMaster_CommForSystem requestDeviceRename:0 withDeviceAccount:self.numStr withDeviceName:_nameText.text];
    
//    [GBInterMaster_CommForSystem requestDeviceRename:0 withDeviceAccount:[[NSUserDefaults standardUserDefaults] objectForKey:@"account"] withDeviceName:_nameText.text];
}

- (void)backBtnClick
{
    if (self.backBlock) {
        self.backBlock();
    }
}

- (void)setEquipAlisa:(NSNotification *)notification
{
    id userInfo = [notification userInfo];
    if(userInfo == nil){
        return;
    }
    NSDictionary * result = userInfo;
    int msgNumber = [[result objectForKey:@"msgNumber"] intValue];
    int responseCode = [[result objectForKey:@"responseCode"] intValue];
    NSLog(@"setEquipAlisa == 结果，消息序号：%d，响应码：%x",msgNumber,responseCode);
    if(responseCode == 0x00)
    {
        if (self.makeSureBlock) {
            self.makeSureBlock();
        }
    }else
    {
        [SPResponseCodePrompt responseCodePrompt:responseCode];
    }
}
#pragma mark UITextFieldDelegate

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    [self layoutIfNeeded];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight );
    return YES;
}
//#pragma -mark 输入框代理

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    [self layoutIfNeeded];
    CGFloat keyboardHeight = 216.0f;
    self.frame = CGRectMake(0, -40 , kScreenWidth, kScreenHeight);
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [self layoutIfNeeded];
    self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    self.frame = CGRectMake(0, 0 , kScreenWidth, kScreenHeight);
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
